<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12" >
            <a-form-model-item label="仪表编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ref">
              <a-input v-model="model.ref" placeholder="请输入仪表编号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="仪表名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入仪表名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="仪表类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="list" v-model="model.type" dictCode="instrument_type" placeholder="请选择仪表类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="position">
              <a-input v-model="model.position" placeholder="请输入位置" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="用途" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="purpose">
              <a-input v-model="model.purpose" placeholder="请输入用途" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="网关" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="gateway">
              <a-input v-model="model.gateway" placeholder="请输入网关" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="单元分区" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="zone">
              <j-tree-select
                ref="treeSelect"
                placeholder="请选择分区"
                v-model="model.zone"
                dict="ene_workshop,name,id"
                pidField="pid"
                pidValue="0"
                hasChildField="has_child"
              >
              </j-tree-select>
            </a-form-model-item>
          </a-col>
       
          <a-col :span="12" >
            <a-form-model-item label="虚拟表" :labelCol="labelCol" :wrapperCol="wrapperCol"  prop="virtuals">
              <j-dict-select-tag type="radio" v-model="model.virtuals"   dictCode="yn" placeholder="虚拟表" @change="addBefore"/>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="12" >
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <a-input-number v-model="model.status" placeholder="请输入状态" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12" >
            <a-form-model-item label="倍率" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="magnif">
              <a-input-number v-model="model.magnif" placeholder="请输入倍率" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="自动抄录" :labelCol="labelCol" :wrapperCol="wrapperCol"  prop="copy">
              <j-dict-select-tag type="radio" v-model="model.copy"   dictCode="yn" placeholder="自动抄录" @change="addBefore"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="notes">
              <a-textarea v-model="model.notes" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
<!--    <a-tabs v-model="activeKey" @change="handleChangeTabs">-->
<!--      <a-tab-pane tab="ene_instrument_rule" :key="refKeys[0]" :forceRender="true">-->
<!--        <j-vxe-table-->
<!--          keep-source-->
<!--          :ref="refKeys[0]"-->
<!--          :loading="eneInstrumentRuleTable.loading"-->
<!--          :columns="eneInstrumentRuleTable.columns"-->
<!--          :dataSource="eneInstrumentRuleTable.dataSource"-->
<!--          :maxHeight="300"-->
<!--          :disabled="formDisabled"-->
<!--          :rowNumber="true"-->
<!--          :rowSelection="true"-->
<!--          :toolbar="true"-->
<!--          />-->
<!--      </a-tab-pane>-->
<!--    </a-tabs>-->
  </a-spin>
</template>

<script>

import { getAction, httpAction } from '@api/manage'
  import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
  import { JVXETypes } from '@comp/jeecg/JVxeTable'
  import { getRefPromise,VALIDATE_FAILED} from '@comp/jeecg/JVxeTable/utils/vxeUtils.js'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@comp/jeecg/JFormContainer.vue'

  export default {
    name: 'EneInstrumentForm',
    mixins: [JVxeTableModelMixin],
    components: {
      JFormContainer,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        model:{
         },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
           ref: [
              { required: true, message: '请输入仪表编号!'},
           ],
           name: [
              { required: true, message: '请输入仪表名称!'},
           ],
           type: [
              { required: true, message: '请输入仪表类型!'},
           ],
           position: [
              { required: true, message: '请输入位置!'},
           ],
           purpose: [
              { required: true, message: '请输入用途!'},
           ],
           zone: [
              { required: true, message: '请输入用能分区!'},
           ],
        },
        refKeys: ['eneInstrumentRule', ],
        tableKeys:['eneInstrumentRule', ],
        activeKey: 'eneInstrumentRule',
        // ene_instrument_rule
        eneInstrumentRuleTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '运算符',
              key: 'operator',
              type: JVXETypes.select,
              options:[],
              dictCode:"",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '电表',
              key: 'name',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '系数',
              key: 'coefficient',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
          ]
        },
        url: {
          add: "/energy/eneInstrument/add",
          edit: "/energy/eneInstrument/edit",
          queryById: "/energy/eneInstrument/queryById",
          eneInstrumentRule: {
            list: '/energy/eneInstrument/queryEneInstrumentRuleByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      addBefore(){
        this.eneInstrumentRuleTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.eneInstrumentRule.list, params, this.eneInstrumentRuleTable)
        }
      },
      //校验所有一对一子表表单
        validateSubForm(allValues){
            return new Promise((resolve,reject)=>{
              Promise.all([
              ]).then(() => {
                resolve(allValues)
              }).catch(e => {
                if (e.error === VALIDATE_FAILED) {
                  // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                  this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
                } else {
                  console.error(e)
                }
              })
            })
        },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          eneInstrumentRuleList: allValues.tablesValue[0].tableData,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },

      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>

<style scoped>
</style>